#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int longestSubsequence(string s, int k) {
        vector<int> arr;
        int res = 0;
        long long num = 0;
        int left = 0;
        for (int right = 0; right < s.size(); right++)
        {
            num <<= 1;
            num += (s[right] - '0');
            if ((s[right] - '0') != 0)
                arr.push_back(right);
            while (num > k)
            {
                num -= (1 << (right - arr[left]));
                ++left;
            }
            res = max(res, right + 1 - left);
        }
        return res;
    }
};